package com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.service.business.msg.convert;

import com.yonyou.iuap.ipaas.connector.sdk.exception.BusinessModuleException;
import com.yonyou.iuap.ipaas.connector.sdk.exception.CommonErrorTypeDefinition;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.business.msg.BaseToDo;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.business.msg.extend.BipNotify;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.api.model.business.msg.extend.Textcard;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.SdwcoatencentConnection;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.service.core.ParamConvertService;
import com.yonyou.iuap.ipaas.connector.sdwcoatencent.internal.utils.LogUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class NotifyParamConvertService implements ParamConvertService<BaseToDo, BipNotify> {
    @Override
    public List<BaseToDo> convertForBatchSave(List<BipNotify> notifys, SdwcoatencentConnection connection) {
        List<BaseToDo> BaseToDos = new ArrayList();
        String corpid = connection.getCorpid();
        Long agentid = connection.getAgentid();
        String authInterface = connection.getAuthInterface();
        Iterator var7 = notifys.iterator();
        while(var7.hasNext()) {
            BipNotify notify = (BipNotify)var7.next();
            Boolean syncFlag = notify.getSyncFlag();
            StringBuilder authUrlBuilder = new StringBuilder();
            authUrlBuilder.append(authInterface);
            if (syncFlag) {
                List<String> executorIdsList = notify.getExecutorIdsList();
                String executorIds = (String)executorIdsList.stream().collect(Collectors.joining("|"));
                notify.setTouser(executorIds);
                notify.setMsgtype("textcard");
                notify.setAgentid(agentid);
                String webUrl = notify.getWebUrl() == null ? "" : notify.getWebUrl();
                String mUrl = notify.getMUrl() == null ? "" : notify.getMUrl();
                authUrlBuilder.append("/"+notify.getTenantId()+"/"+executorIds);
                try {
                    Textcard card = new Textcard();
                    card.setTitle(notify.getMsgTitle());
                    card.setDescription(notify.getMsgContent());
                    String state = URLEncoder.encode("bizUrl=" + URLEncoder.encode("webUrl=" + URLEncoder.encode(webUrl, "UTF-8") + "&mUrl=" + URLEncoder.encode(mUrl, "UTF-8"), "UTF-8"), "UTF-8");
                    StringBuilder urlBuilder = new StringBuilder();
                    urlBuilder.append(connection.getAuthHost()).append("/connect/oauth2/authorize").append("?appid=").append(corpid).append("&redirect_uri=").append(URLEncoder.encode(authUrlBuilder.toString(), "UTF-8")).append("&response_type=code&scope=snsapi_base&state=").append(state).append("&agentid=").append(agentid).append("#wechat_redirect");
                    card.setUrl(urlBuilder.toString());
                    notify.setTextcard(card);
                    BaseToDos.add(notify);
                } catch (UnsupportedEncodingException var17) {
                    LogUtils.printStacktrace(var17);
                    throw BusinessModuleException.simpleException("999", "build notify error,errorMsg:" + var17.getMessage(), CommonErrorTypeDefinition.CONNECTIVITY);
                }
            }
        }
        return BaseToDos;
    }

    @Override
    public List<BaseToDo> convertForBatchDelete(List<BipNotify> list, SdwcoatencentConnection connection) {
        return null;
    }

    @Override
    public BaseToDo convertForSave(BipNotify objet, SdwcoatencentConnection connection) {
        return null;
    }

    @Override
    public BaseToDo convertForDelete(BipNotify objet, SdwcoatencentConnection connection) {
        return null;
    }
}
